/**
 * @ author: 汪孟苹
 */
define(["jquery", "bbcare"], function ($, $bb) {
    var validate = {};
    
    /**
     * 校验规则
     */
    var validateNoNull = function(params){              // 非空
        var _default = {
            "ruleVal":true,
            "msg":"内容不可以为空！",
        };
        params = $.extend(_default,params);
        if( params.obj.attr("type") == "radio" || params.obj.attr("type") == "checkbox" ){
            var success = $.parseJSON(params.ruleVal) ? ( params.value > 0 ? true : false ): true;
        }else{
            var reg = /\S/;
            var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        }
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateLength = function(params){
        var _default = {
            "ruleVal":11,
            "msg":"请输入11位",
        };
        params = $.extend(_default,params);
        if( params.value == "" ) return true;
        var success = Number(params.value.length) == Number(params.ruleVal) ? true : false;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;  
    }
    var validateNumberStr = function(params){              // 数字字符
        var _default = {
            "ruleVal":true,
            "msg":"请输入数字！",
        };
        params = $.extend(_default,params);
        var reg = /^$|^\d+$/;
        var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateNum = function(params){              // 整数+小数
        var _default = {
            "ruleVal":true,
            "msg":"请输入数字！",
        };
        params = $.extend(_default,params);
        var reg = /^$|^-?\d+(\.\d+)?$/;
        var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateiInteger = function(params){              // 整数
        var _default = {
            "ruleVal":true,
            "msg":"请输入整数！",
        };
        params = $.extend(_default,params);
        var reg = /^$|^-?[1-9]\d*$|^0$/;
        var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateMoreThanNum = function(params){     // 大于一个数   
        var _default = {
            "ruleVal":0,
            "msg":"请输入大于0的数",
        };
        params = $.extend(_default,params);          
        if( params.value == "" ) return true;
        var success = Number(params.value) > Number(params.ruleVal) ? true : false;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateGreaterEqualNum = function(params){     // 大于等于一个数
        var _default = {
            "ruleVal":0,
            "msg":"请输入大于等于0的数",
        };
        params = $.extend(_default,params);        
        if( params.value == "" ) return true;
        var success = Number(params.value) >= Number(params.ruleVal) ? true : false;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateLessThanNum = function(params){     // 小于一个数   
        var _default = {
            "ruleVal":0,
            "msg":"请输入小于0的数",
        };
        params = $.extend(_default,params);          
        if( params.value == "" ) return true;
        var success = Number(params.value) < Number(params.ruleVal) ? true : false;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateLessThanEqualNum = function(params){     // 小于等于一个数   
        var _default = {
            "ruleVal":0,
            "msg":"请输入小于等于0的数",
        };
        params = $.extend(_default,params);        
        if( params.value == "" ) return true;
        var success = Number(params.value) <= Number(params.ruleVal) ? true : false;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateDecimals = function(params){               // 精确到小数点后rule位
        var _default = {
            "ruleVal":1,
            "msg":"请精确到小数点后1位",
        };
        params = $.extend(_default,params); 
        var reg = new RegExp("^$|^-?[0-9]+(\.[0-9]{"+params.ruleVal+"})$"); 
        var success = reg.test(params.value);
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validateMaxDecimals = function(params){               // 精确到小数点后最多rule位
        var _default = {
            "ruleVal":3,
            "msg":"请精确到小数点后最多3位",
        };
        params = $.extend(_default,params); 
        var reg = new RegExp("^$|^-?[0-9]+(\.[0-9]{1,"+params.ruleVal+"})$"); 
        var success = reg.test(params.value);
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    var validatePhone = function(params){               // 手机号码
        var _default = {
            "ruleVal":true,
            "msg":"请输入正确的手机号码！",
        };
        params = $.extend(_default,params); 
    	var reg = /^$|^1[3456789]\d{9}$/;
    	var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        validateTip(success,params.msg,params.obj,params.focus);
        return success;
    }
    
    /**
     * 没通过校验的提示信息
     */
    var validateTip = function(success,msg,obj,focus){
        if(!success){
            $bb.toastShow( msg , 2000 );
            if(focus){
                obj.focus();
            }
        } 
    }
    /**
     * 校验处理
     */
    var validateHandle = function(obj){
        var validateJson = obj.attr("data-validate");
        if(!validateJson || validateJson.trim().length == 0) return true;
    	var options = JSON.parse( validateJson );
		var value = obj.val().trim(); // 获取输入的值
        if( typeof(obj.attr("type")) != "undefined" ){
            var type = obj.attr("type");
            if( type == "radio" ){
                value = obj.parents(".radio").find("input[type=radio]:checked").length;
            }else if( type == "checkbox" ){
                value = obj.parents(".checkbox").find("input[type=checkbox]:checked").length;
            }
        }
		if(!options || options.length == 0) return true;
        var result = true;
        for (var i = 0; i < options.length; i++) {
            if(!options[i].focus) options[i].focus = false;
            var params = {
                "value":value,
                "ruleVal":options[i].val,
                "msg":options[i].msg,
                "obj":obj,
                "focus":options[i].focus
            }
            switch(options[i].rule) {
                case "noNull":
                    result = result && validateNoNull(params);
                    break;
                case "length":
                    result = result && validateLength(params);
                    break;
                case "numberStr":
                    result = result && validateNumberStr(params);
                    break;
                case "num":
                    result = result && validateNum(params);
                    break;
                case "integer":
                    result = result && validateiInteger(params);
                    break;
                case "moreThanNum":
                    result = result && validateMoreThanNum(params);
                    break;
                case "greaterEqualNum":
                    result = result && validateGreaterEqualNum(params);
                    break;
                case "lessThanNum":
                    result = result && validateLessThanNum(params);
                    break;
                case "lessThanEqualNum":
                    result = result && validateLessThanEqualNum(params);
                    break;
                case "decimals":
                    result = result && validateDecimals(params);
                    break;
                case "maxDecimals":
                    result = result && validateMaxDecimals(params);
                    break;
                case "phone":
                    result = result && validatePhone(params);
                    break;
            }
        }
        return result;
    }
    /**
     * 失去焦点事件
     */
    var onBlur = function(){
        var attentionFun = $(this).data("attentionFun");  // 提示警告框变黄
        if(attentionFun){
            attentionFun($(this));
        }

        var fun = $(this).data("fun");
        var success = validateHandle($(this));
        if(success&&fun){
            fun.call(this);
        }
    }
    /**
     * 失去焦点时触发
     */
    validate.blurInit = function(){
        $('.validatebox').unbind("blur");
    	$('.validatebox').blur(onBlur);
    }
    /**
     * 提交时触发（事件在用的时候写）
     */
    validate.init = function(){
        var result = true;
    	$('.validatebox').each(function(i){
            var obj = $('.validatebox').eq(i);
    		var success = validateHandle(obj);
            result = result && success;
            if(!result) return result;
    	});
        return result;
    }

    return validate;
});